package com.chenyaowei.order.service.impl;

import com.chenyaowei.common.core.domain.AjaxResult;
import com.chenyaowei.common.core.domain.entity.SysUser;
import com.chenyaowei.common.utils.SecurityUtils;
import com.chenyaowei.member.service.ICywMemberStatisticsInfoService;
import com.chenyaowei.order.domain.CywOrderItem;
import com.chenyaowei.order.mapper.CywOrderItemMapper;
import com.chenyaowei.order.request.OrderCommentReplyRequest;
import com.chenyaowei.order.request.OrderCommentRequest;
import com.chenyaowei.order.service.ICywOrderItemService;
import com.chenyaowei.product.domain.CywProSpuComment;
import com.chenyaowei.product.domain.CywProSpuCommentReply;
import com.chenyaowei.product.service.ICywProSpuCommentReplyService;
import com.chenyaowei.product.service.ICywProSpuCommentService;
import com.chenyaowei.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 订单项信息Service业务层处理
 *
 * @author 陈耀威
 * @date 2023-12-03
 */
@Service
public class CywOrderItemServiceImpl implements ICywOrderItemService
{
    @Autowired
    private CywOrderItemMapper cywOrderItemMapper;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ICywProSpuCommentService commentService;

    @Autowired
    private ICywProSpuCommentReplyService commentReplyService;

    @Autowired
    private ICywMemberStatisticsInfoService memberStatisticsInfoService;

    /**
     * 查询订单项信息
     *
     * @param id 订单项信息主键
     * @return 订单项信息
     */
    @Override
    public CywOrderItem selectCywOrderItemById(Long id)
    {
        return cywOrderItemMapper.selectCywOrderItemById(id);
    }

    /**
     * 查询订单项信息列表
     *
     * @param cywOrderItem 订单项信息
     * @return 订单项信息
     */
    @Override
    public List<CywOrderItem> selectCywOrderItemList(CywOrderItem cywOrderItem)
    {
        return cywOrderItemMapper.selectCywOrderItemList(cywOrderItem);
    }

    /**
     * 新增订单项信息
     *
     * @param cywOrderItem 订单项信息
     * @return 结果
     */
    @Override
    public int insertCywOrderItem(CywOrderItem cywOrderItem)
    {
        return cywOrderItemMapper.insertCywOrderItem(cywOrderItem);
    }

    /**
     * 修改订单项信息
     *
     * @param cywOrderItem 订单项信息
     * @return 结果
     */
    @Override
    public int updateCywOrderItem(CywOrderItem cywOrderItem)
    {
        return cywOrderItemMapper.updateCywOrderItem(cywOrderItem);
    }

    /**
     * 批量删除订单项信息
     *
     * @param ids 需要删除的订单项信息主键
     * @return 结果
     */
    @Override
    public int deleteCywOrderItemByIds(Long[] ids)
    {
        return cywOrderItemMapper.deleteCywOrderItemByIds(ids);
    }

    /**
     * 删除订单项信息信息
     *
     * @param id 订单项信息主键
     * @return 结果
     */
    @Override
    public int deleteCywOrderItemById(Long id)
    {
        return cywOrderItemMapper.deleteCywOrderItemById(id);
    }

    @Override
    public void saveBatch(List<CywOrderItem> orderItems) {
        cywOrderItemMapper.saveBatch(orderItems);
    }

    @Override
    public List<CywOrderItem> selectListByOrderSn(String orderSn) {
        return cywOrderItemMapper.selectListByOrderSn(orderSn);
    }

    @Override
    public List<CywOrderItem> selectListByOrderSns(List<String> orderSns) {
        return cywOrderItemMapper.selectListByOrderSns(orderSns);
    }

    @Transactional
    @Override
    public AjaxResult addComment(OrderCommentRequest orderCommentRequest) {
        CywOrderItem orderItem = cywOrderItemMapper.selectCywOrderItemById(orderCommentRequest.getOrderItemId());
        SysUser user = userService.selectUserById(SecurityUtils.getUserId());
        CywProSpuComment comment = new CywProSpuComment();
        BeanUtils.copyProperties(orderCommentRequest, comment);
        comment.setOrderId(orderItem.getOrderId());
        comment.setSkuId(orderItem.getSkuId());
        comment.setSpuId(orderItem.getSpuId());
        comment.setSkuAttributes(orderItem.getSkuAttrsVals());
        comment.setSpuName(orderItem.getSpuName());
        Long userId = user.getUserId();
        comment.setMemberId(userId);
        comment.setMemberNickName(user.getNickName());
        comment.setMemberIcon(user.getAvatar());
        comment.setCreateTime(new Date());
        // 联合唯一索引 uk_index_sku_id_member_id_order_id
        int count = commentService.insertCywProSpuComment(comment);
        if (count == 1) {
            orderItem.setCommentTime(new Date());
            orderItem.setCommentAllow(false);
            cywOrderItemMapper.updateCywOrderItem(orderItem);
            memberStatisticsInfoService.updateCommentCount(userId, 1);
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @Override
    public int addCommentReply(OrderCommentReplyRequest orderCommentReplyRequest) {
        SysUser user = userService.selectUserById(SecurityUtils.getUserId());
        CywProSpuCommentReply commentReply = new CywProSpuCommentReply();
        commentReply.setMemberId(user.getUserId());
        commentReply.setMemberNickName(user.getNickName());
        commentReply.setMemberIcon(user.getAvatar());
        commentReply.setCreateTime(new Date());
        commentReply.setContent(orderCommentReplyRequest.getContent());
        commentReply.setResources(orderCommentReplyRequest.getResources());
        commentReply.setCommentId(orderCommentReplyRequest.getCommentId());
        return commentReplyService.insertCywProSpuCommentReply(commentReply);
    }
}
